<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Authentication Model
 * 
 * @package     Authentican
 * @category    Library
 * @author      Gene Bryan Ronquillo
 * @link        http://generonquillo.com
 * @version     1.0
 */

class Auth_model extends CI_Model {
    
    public $user_table = 'users';
    
    // ------------------------------------------------------------------------
    
    /**
     * Construct
     * 
     */
    public function __construct()
    {
        parent::__construct();
    }
    
    // ------------------------------------------------------------------------
    
    /**
     * Hashes a string with salt
     *
     */
    public function _hash_string($str = '', $salt = '')
    {
        if (! $str) return FALSE;
        
        return sha1(md5($str) . $salt);
    }
    
    // ------------------------------------------------------------------------
    
    /**
     * Create random salt value
     * 
     */
    public function _salt($len = 16)
    {
        $this->load->helper('string');
        return random_string('alnum', $len);
    }
    
    // ------------------------------------------------------------------------
    
    public function register($fields = '', $others = '')
    {
        $salt       = $this->_salt();
        $password   = $this->_hash_string($fields['password'], $salt);
        $date       = date('Y-m-d H:i:s');
        
        $data = array(
            // 'username'      => $fields['username'],
            'password'      => $password,
            'email'         => $fields['email'],
            'salt'          => $salt,
            'created'       => $date
        );
        
        $user_data = array_merge($others, $data);
        $this->db->insert($this->user_table, $user_data);
        
        return $this->db->affected_rows() > 0 ? $this->get_user($this->db->insert_id()) : FALSE;
    }
    
    // ------------------------------------------------------------------------
    
    public function get_user($value = '', $field = 'user_id', $params = '*')
    {
        if (($field == 'user_id') && ($value == ''))
        {
            // get logged in user_id
            $value = $this->session->userdata('user_id');
        }
        
        $query = $this->db->select($params)
                          ->where($field, $value)
                          ->where('deleted', 'n')
                          ->get($this->user_table, 1);
                          
        return $query->num_rows() > 0 ? $query->row_array() : FALSE;
    }
    
    // ------------------------------------------------------------------------
    
    public function login($fields)
    {
        $user       = $this->get_user($fields['email'], 'email', 'salt');
        $password   = $this->_hash_string($fields['password'], $user['salt']);
        
        $query = $this->db->where('email', $fields['email'])
                          ->where('password', $password)
                          ->where('deleted', 'n')
                          ->get($this->user_table, 1);
        
        return $query->num_rows() > 0 ? $query->row_array() : FALSE;
    }
    
    // ------------------------------------------------------------------------
    
    public function update_last_activity($user_id = '')
    {
        $data['last_activity'] = date('Y-m-d H:i:s');
        
        $query = $this->db->where('user_id', $user_id)
                          ->where('deleted', 'n')
                          ->update($this->user_table, $data);
                          
        return $query;
    }
    
    // ------------------------------------------------------------------------
    
    public function update_password($password)
    {
        $user = $this->get_user();
        $pass = $this->_hash_string($password, $user['salt']);
        
        $query = $this->db->where('user_id', $user['user_id'])
                          ->where('deleted', 'n')
                          ->update($this->user_table, array('password' => $pass));
        
        return $query;
    }
    
    // ------------------------------------------------------------------------
	
	public function list_roles()
	{
		$query = $this->db->where('deleted', 'n')
						  ->get('roles');
						  
		return $query->num_rows() > 0 ? $query->result_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
}

/* End of file auth_model.php */
/* Location: ./application/models/auth_model.php */